const {
	Service
} = require("uni-cloud-router");
const {
	result_success,
	result_error
} = require('http-result-modle');
const db = uniCloud.database();
const $ = db.command.aggregate;
const uidObj = require('uni-id');
module.exports = class ChannelService extends(
	Service
) {

	async list(event) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			response.msg = '当前未登录'
			return response;
		}
		const cMemberRes = await this.db.collection('wmzmall-member').doc(uid).get()
		let my_invitation_code = cMemberRes.data[0].my_invitation_code
		const memberRes = await this.db.collection('wmzmall-member').where({
			invitation_code: my_invitation_code
		}).field({
			market_level_name: true,
			member_name: true,
			member_mobile: true,
			member_headimg: true,
			create_time: true
		}).orderBy('create_time', 'desc').get()
		return result_success(memberRes.data);
	}
	async order(event) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			response.msg = '当前未登录'
			return response;
		}
		const cMemberRes = await this.db.collection('wmzmall-member').doc(uid).get()
		let app_id = cMemberRes.data[0].app_id
		let shop_id = cMemberRes.data[0].shop_id
		const orderRes = await this.db.collection('wmzmall-order').where({
			app_id: app_id,
			shop_id: shop_id,
			order_type: '云仓订单'
		}).orderBy('create_time', 'desc').get()
		return result_success(orderRes.data);
	}
	async order_th(event) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			response.msg = '当前未登录'
			return response;
		}
		const cMemberRes = await this.db.collection('wmzmall-member').doc(uid).get()
		let app_id = cMemberRes.data[0].app_id
		let shop_id = cMemberRes.data[0].shop_id
		const orderRes = await this.db.collection('wmzmall-order').where({
			app_id: app_id,
			shop_id: shop_id,
			order_type: '发货订单'
		}).orderBy('create_time', 'desc').get()
		return result_success(orderRes.data);
	}
};